package com.example.bookManagement.Controller;

import com.example.bookManagement.Model.Constants;
import com.example.bookManagement.Model.Result;
import com.example.bookManagement.Model.UserInfo;
import com.example.bookManagement.Service.UserService;
import jakarta.servlet.http.HttpSession;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/login",produces = "application/json")
    public Result login(String username, String password, HttpSession session) {
       if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
           return Result.fail("用户名或密码为空！");
       }
       UserInfo userInfo = userService.queryUserByName(username);
       if(userInfo == null) {
           return Result.fail("用户不存在!");
       }
       if(!password.equals(userInfo.getPassword())) {
           return Result.fail("密码错误！");
       }
       session.setAttribute(Constants.SESSION_USER_KEY, userInfo);
       return Result.success("");
    }
}
